/**
 * Copyright 2009 University of  Washington
 * This code is released under the GPL version 3 - with a copy of the license included
 * @author Maya Hao Li
 *
 */
package seedpod.webapp.view.htmlwidget;

import java.util.Vector;

public class TableRow extends GenericHtmlWidget {
	private Vector<TableCell> _cells;

	private int _rowIndex;

	private Table _table;

	private String _rowColor = "";

	private String _onMouseOverCssClass = "";

	protected int _numCells = 0;

	private boolean _isHeader = false;

	public TableRow(Table table, int index) {
		super();
		_table = table;
		_rowIndex = index;
		_cells = new Vector<TableCell>();
	}

	public int getRowIndex() {
		return _rowIndex;
	}

	public TableCell addCell( String w ) {
		TableCell newCell = new TableCell( w );
		_cells.add( newCell );
		_numCells++;
		return newCell;
	}
	
	public TableCell addCell(Object w ) {
		return addCell(w.toString());
	}

	public void setRowColor( String rowColor ) {
		_rowColor = rowColor;
	}

	public void setIsHeader( boolean isHeader ) {
		_isHeader = isHeader;
	}

	public boolean isHeader() {
		return _isHeader;
	}

	public String getRowColor() {
		return _rowColor;
	}

	public void setOnMouseOverCssClass( String className ) {
		_onMouseOverCssClass = className;
	}
	
	public String getOnMouseOverCssClass() {
		return _onMouseOverCssClass;
	}

	public String render() {
		StringBuffer tr = new StringBuffer();
		tr.append( "<tr " );
		tr.append( ATT( "class", _cssClass ) );
		tr.append( ATT( "bgcolor", _rowColor ) );

		if (!_isHeader && _onMouseOverCssClass.length() > 0 ) {
			tr.append( ATT( " onMouseOver", "this.className='" + _onMouseOverCssClass + "' " ) );
			tr.append( ATT( " onMouseOut", "this.className='" + _cssClass + "' " ) );
		}
		tr.append( ">" );

		for ( int c = 0; c < _numCells; c++ ) {
			TableCell cell = (TableCell)_cells.get( c );
			if ( c == (_numCells - 1) && _numCells < _table._maxCellsPerRow )
				cell.setColSpan( _table._maxCellsPerRow - _numCells + 1 );

			if ( !cell.isHeader() ) 
				cell.setIsHeader( _isHeader );
			tr.append( cell.render() );
		}
		tr.append( "</tr>" );
		return tr.toString();
	}

}
